package view
{
	import com.bit101.components.VBox;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	public class ListButton extends Sprite
	{
		private var data:Object;
		/*[Embed(source="assets/navigation/sizeTypeBg.png")]
		private var bgCls:Class;
		private var bgBitmpa;*/
		private var titleTxt:TextField;
		private var txtBtnContainer:Sprite;
		private var _selected:Boolean = false;
		private var shape:Shape;
		private var model:ModelLocator = ModelLocator.getInstance();
		public function ListButton()
		{
			init();
		}
		private function init():void{
			/*bgBitmpa = new bgCls();
			this.addChild(bgBitmpa);*/
			titleTxt = new TextField;
			titleTxt.width = 66;
			titleTxt.height = 22;
			titleTxt.selectable = false;
			this.addChild(titleTxt);
			txtBtnContainer = new Sprite;
			txtBtnContainer.y = 24;
			//this.addChild(txtBtnContainer);
			shape = new Shape
			shape.x = 66;
			shape.y = 7;
			this.addChild(shape);
		}
		public function setData(obj:Object):void{
			this.data = obj;
			titleTxt.htmlText = "<a href='event:myText'><font color='#5e5e5e'><b>" + obj.name + "</b></font></a>";
			titleTxt.x = 8;
			titleTxt.addEventListener(MouseEvent.CLICK,titleClick);
			var ary:Array = obj.data;
			for(var i:int;i<ary.length;i++){
				var txt:TextDataFiled = new TextDataFiled;
				txt.width = 132;
				txt.height = 22;
				txt.y = 22 * i;
				txt.data = ary[i];
				txt.htmlText = "<p align='center'><a href='event:myText'><font color='#5e5e5e'>" + ary[i].w + "×" +  ary[i].h + "</font></a></p>";
				txtBtnContainer.addChild(txt);
				txt.addEventListener(MouseEvent.CLICK,onClick);
			}
			//drawRec(true);
			drawShpe(false);
			this._selected = false;
			//this.selected = false;
		}
		private function titleClick(event:MouseEvent):void{
			this.selected = !this._selected;
		}
		public function set selected(value:Boolean):void{
			this._selected = value;
			if(value){
				if(!txtBtnContainer.parent)
					this.addChild(txtBtnContainer);
			}else{
				if(txtBtnContainer.parent)
					this.removeChild(txtBtnContainer);
			}
			drawRec(value);
			drawShpe(value);
			ProportionList(this.parent).resize();
			SizeShow.getInstance().clear();
			SizeShow.getInstance().drawRec(this.data.data);
		}
		public function get selected():Boolean{
			return _selected;
		}
		private function onClick(event:MouseEvent):void{
			if(model.currentListBtn && model.currentListBtn != this){
				SizeShow.getInstance().clear();
				SizeShow.getInstance().drawRec(this.data.data);
			}
			//var str:String = event.target.text;
			//var ary:Array = str.split("×");
			var data:Object = event.target.data;
			SizeShow.getInstance().onClick(data.w,data.h);
			model.size = data.w + "X" + data.h;
			PricePanle.getInstance().setPriceType(data); 
			loadType(model.size);
			model.currentListBtn = this;
		}
		private function loadType(str:String):void{
			if(model.boderAllAry){
				model.setNewBorderAry(model.size);
			}else{
				var urlloader:URLLoader = new URLLoader();
				urlloader.addEventListener(Event.COMPLETE,onComplete)
				str = model.config.type //+ "?size=" + str;
				urlloader.load(new URLRequest(str));
				model.isLoaderboderAry = true;
			}
		}
		private function onComplete(event:Event):void{
			var xml:XML = XML(event.target.data);
			model.setBoderAry(xml);
			model.setNewBorderAry(model.size);
		}
		private function drawRec(value:Boolean):void{
			var g:Graphics = this.graphics;
			g.clear();
			if(!value)
				return;
			g.lineStyle(1,0x999999,0.8);
			g.moveTo(0,0);
			g.lineTo(80,0);
			g.lineTo(80,20);
			g.lineTo(132,20);
			g.lineTo(132,this.height);
			g.lineTo(0,this.height);
			g.lineTo(0,0);
		}
		private function drawShpe(value:Boolean):void{
			var g:Graphics = shape.graphics;
			g.clear();
			g.beginFill(0x999999);
			if(value){
				g.moveTo(4,0);
				g.lineTo(0,6)
				g.lineTo(8,6);
				g.lineTo(4,0);
			}else{
				g.moveTo(0,0);
				g.lineTo(8,0)
				g.lineTo(4,6);
				g.lineTo(0,0);
			}
				
		}
	}
}